package com.lineage.server.datatables;

import com.lineage.DatabaseFactory;
import com.lineage.server.model.Instance.L1ItemInstance;
import com.lineage.server.model.Instance.L1PcInstance;
import com.lineage.server.serverpackets.S_ItemStatus;
import com.lineage.server.serverpackets.S_ServerMessage;
import com.lineage.server.serverpackets.S_SystemMessage;
import com.lineage.server.serverpackets.ServerBasePacket;
import com.lineage.server.utils.SQLUtil;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class RewardWeaponTable {
  private static final Log _log = LogFactory.getLog(RewardWeaponTable.class);
  
  public static boolean START;
  
  private static ArrayList<ArrayList<Object>> _array = new ArrayList<>();
  
  private static RewardWeaponTable _instance;
  
  static {
    START = false;
  }
  
  private RewardWeaponTable() {
    getData();
    _log.info("武器等级伤害->" + _array.size());
    if (_array.size() <= 0) {
      _array.clear();
      _array = null;
    } else {
      START = true;
    } 
  }
  
  public static RewardWeaponTable get() {
    if (_instance == null)
      _instance = new RewardWeaponTable(); 
    return _instance;
  }
  
  public static void forWeapon(L1PcInstance pc, L1ItemInstance item, int weapon_level, boolean equipped) {
    ArrayList<Object> aTempData = null;
    if (_array == null)
      return; 
    if (equipped) {
      int[] data = new int[14];
      int i = 0;
      while (i < _array.size()) {
        aTempData = _array.get(i);
        if (weapon_level == ((Integer)aTempData.get(0)).intValue() && 
          item.getItem().getType() == ((Integer)aTempData.get(14)).intValue()) {
          if (((Integer)aTempData.get(1)).intValue() != 0)
            data[0] = ((Integer)aTempData.get(1)).intValue(); 
          if (((Integer)aTempData.get(2)).intValue() != 0)
            data[1] = ((Integer)aTempData.get(2)).intValue(); 
          if (((Integer)aTempData.get(3)).intValue() != 0)
            data[2] = ((Integer)aTempData.get(3)).intValue(); 
          if (((Integer)aTempData.get(4)).intValue() != 0)
            data[3] = ((Integer)aTempData.get(4)).intValue(); 
          if (((Integer)aTempData.get(5)).intValue() != 0)
            data[4] = ((Integer)aTempData.get(5)).intValue(); 
          if (((Integer)aTempData.get(6)).intValue() != 0)
            data[5] = ((Integer)aTempData.get(6)).intValue(); 
          if (((Integer)aTempData.get(7)).intValue() != 0)
            data[6] = ((Integer)aTempData.get(7)).intValue(); 
          if (((Integer)aTempData.get(8)).intValue() != 0)
            data[7] = ((Integer)aTempData.get(8)).intValue(); 
          if (((Integer)aTempData.get(9)).intValue() != 0)
            data[8] = ((Integer)aTempData.get(9)).intValue(); 
          if (((Integer)aTempData.get(10)).intValue() != 0)
            data[9] = ((Integer)aTempData.get(10)).intValue(); 
          if (((Integer)aTempData.get(11)).intValue() != 0)
            data[10] = ((Integer)aTempData.get(11)).intValue(); 
          if (((Integer)aTempData.get(12)).intValue() != 0)
            data[11] = ((Integer)aTempData.get(12)).intValue(); 
          if (((Integer)aTempData.get(13)).intValue() != 0)
            data[12] = ((Integer)aTempData.get(13)).intValue(); 
          if (((Integer)aTempData.get(15)).intValue() != 0)
            data[13] = ((Integer)aTempData.get(15)).intValue(); 
          if (data[0] != 0) {
            pc.setweapondmg(data[0]);
            pc.sendPackets((ServerBasePacket)new S_SystemMessage("\\aL固定伤害增加+" + data[0] + "。"));
          } 
          if (data[1] != 0) {
            pc.setDmgdouble(data[1]);
            pc.sendPackets((ServerBasePacket)new S_SystemMessage("\\aL物理伤害增加* " + data[1] + " %"));
          } 
          if (data[9] != 0) {
            pc.addDmgup(data[9]);
            pc.sendPackets((ServerBasePacket)new S_SystemMessage("\\aL近距离伤害+ " + data[9]));
          } 
          if (data[10] != 0) {
            pc.addBowDmgup(data[10]);
            pc.sendPackets((ServerBasePacket)new S_SystemMessage("\\aL远距离伤害+ " + data[10]));
          } 
          if (data[11] != 0) {
            pc.addHitup(data[11]);
            pc.sendPackets((ServerBasePacket)new S_SystemMessage("\\aL近距离命中+ " + data[11]));
          } 
          if (data[12] != 0) {
            pc.addBowHitup(data[12]);
            pc.sendPackets((ServerBasePacket)new S_SystemMessage("\\aL远距离命中+ " + data[12]));
          } 
          if (data[13] != 0) {
            pc.addSp(data[13]);
            pc.sendPackets((ServerBasePacket)new S_SystemMessage("\\aL魔攻+ " + data[13]));
          } 
          if (data[4] != 0) {
            if (data[5] != 0 && data[6] != 0) {
              if (item.getItem().getType1() == 20) {
                pc.setelfweapon(data[6]);
                pc.sendPackets((ServerBasePacket)new S_ServerMessage(166, 
                      "\\aL" + ((data[4] - data[8]) / 10) + "%机率造成伤害:" + data[5] + "~" + data[6]));
              } else {
                pc.sendPackets((ServerBasePacket)new S_ServerMessage(166, 
                      "\\aL" + (data[4] / 10) + "%机率造成伤害:" + data[5] + "~" + data[6]));
              } 
            } else {
              pc.sendPackets((ServerBasePacket)new S_ServerMessage(166, "\\aL" + (data[4] / 10) + "%机率造成伤害:1~" + data[6]));
            } 
            if (data[2] != 0)
              pc.sendPackets((ServerBasePacket)new S_SystemMessage("\\aL吸取血量增加" + data[2] + "点。")); 
            if (data[3] != 0)
              pc.sendPackets((ServerBasePacket)new S_SystemMessage("\\aL吸取魔量增加 " + data[3] + "点。")); 
          } 
          pc.setReward_Weapon(data);
          pc.sendPackets((ServerBasePacket)new S_ItemStatus(item));
        } 
        i++;
      } 
    } else {
      int[] data2 = pc.getReward_Weapon();
      if (data2[0] != 0) {
        pc.setweapondmg(0);
        pc.sendPackets((ServerBasePacket)new S_SystemMessage("\\aH固定伤害减少 " + data2[0] + "。"));
      } 
      if (data2[1] != 0) {
        pc.setDmgdouble(0.0D);
        pc.sendPackets((ServerBasePacket)new S_SystemMessage("\\aH物理伤害减少* " + data2[1] + " %"));
      } 
      if (data2[9] != 0) {
        pc.addDmgup(-data2[9]);
        pc.sendPackets((ServerBasePacket)new S_SystemMessage("\\aL近距离伤害- " + data2[9]));
      } 
      if (data2[10] != 0) {
        pc.addBowDmgup(-data2[10]);
        pc.sendPackets((ServerBasePacket)new S_SystemMessage("\\aL远距离伤害- " + data2[10]));
      } 
      if (data2[11] != 0) {
        pc.addHitup(-data2[11]);
        pc.sendPackets((ServerBasePacket)new S_SystemMessage("\\aL近距离命中- " + data2[11]));
      } 
      if (data2[12] != 0) {
        pc.addBowHitup(-data2[12]);
        pc.sendPackets((ServerBasePacket)new S_SystemMessage("\\aL远距离命中- " + data2[12]));
      } 
      if (data2[13] != 0) {
        pc.addSp(-data2[13]);
        pc.sendPackets((ServerBasePacket)new S_SystemMessage("\\aL魔攻- " + data2[13]));
      } 
      if (data2[4] != 0) {
        if (data2[5] != 0 && data2[6] != 0) {
          if (item.getItem().getType1() == 20) {
            pc.setelfweapon(-data2[6]);
            pc.sendPackets((ServerBasePacket)new S_ServerMessage(166, 
                  "\\aH" + ((data2[4] - data2[8]) / 10) + "%机率造成伤害:" + data2[5] + "~" + data2[6] + "效果消失"));
          } else {
            pc.sendPackets((ServerBasePacket)new S_ServerMessage(166, 
                  "\\aH" + (data2[4] / 10) + "%机率造成伤害:" + data2[5] + "~" + data2[6] + "效果消失"));
          } 
        } else {
          pc.sendPackets(
              (ServerBasePacket)new S_ServerMessage(166, "\\aH" + (data2[4] / 10) + "%机率造成伤害:1~" + data2[6] + " 效果消失"));
        } 
        if (data2[2] != 0)
          pc.sendPackets((ServerBasePacket)new S_SystemMessage("\\aH吸取血量减少 " + data2[2] + "点。")); 
        if (data2[3] != 0)
          pc.sendPackets((ServerBasePacket)new S_SystemMessage("\\aH吸取魔量减少 " + data2[3] + "点。")); 
      } 
      data2 = new int[14];
      pc.setReward_Weapon(data2);
      pc.sendPackets((ServerBasePacket)new S_ItemStatus(item));
    } 
  }
  
  private void getData() {
    Connection cn = null;
    Statement ps = null;
    ResultSet rset = null;
    try {
      cn = DatabaseFactory.get().getConnection();
      ps = cn.createStatement();
      rset = ps.executeQuery("SELECT * FROM w_过安定武器");
      while (rset.next()) {
        ArrayList<Object> aReturn = new ArrayList();
        aReturn.add(0, new Integer(rset.getInt("Level")));
        aReturn.add(1, new Integer(rset.getInt("Dmg")));
        aReturn.add(2, new Integer(rset.getInt("Dmg_double")));
        aReturn.add(3, new Integer(rset.getInt("C_Hp")));
        aReturn.add(4, new Integer(rset.getInt("C_Mp")));
        aReturn.add(5, new Integer(rset.getInt("random")));
        aReturn.add(6, new Integer(rset.getInt("min_dmg")));
        aReturn.add(7, new Integer(rset.getInt("max_dmg")));
        aReturn.add(8, new Integer(rset.getInt("gfxid")));
        aReturn.add(9, new Integer(rset.getInt("random_elf")));
        aReturn.add(10, new Integer(rset.getInt("近距离攻击")));
        aReturn.add(11, new Integer(rset.getInt("远距离攻击")));
        aReturn.add(12, new Integer(rset.getInt("近距离命中")));
        aReturn.add(13, new Integer(rset.getInt("远距离命中")));
        aReturn.add(14, new Integer(rset.getInt("武器类别")));
        aReturn.add(15, new Integer(rset.getInt("sp")));
        _array.add(aReturn);
      } 
    } catch (SQLException e) {
      _log.error(e.getLocalizedMessage(), e);
      return;
    } finally {
      SQLUtil.close(rset);
      SQLUtil.close(ps);
      SQLUtil.close(cn);
    } 
  }
}
